#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>

int main()
{
  pid_t id = fork();
  if(id<0){
    perror("fork error!\n");
    return 1;
  }
  else if(id == 0){
    //child
    int count = 0;
    while(count<5){
      printf("child pid: %d ppid: %d is running ...\n", getpid(), getppid());
      sleep(3);
      count++;
    }
    exit(1);
  }
  else{
  //father
  // printf("father wait before!\n");
  while(1){
    int status = 0;
    pid_t ret = waitpid(id, &status, WNOHANG);
    //pid_t ret = wait(NULL);//ret id 
    
    if(ret>0){
     // printf("wait success! ret = %d\n", ret);
     // printf("exit status: %d\n", status);
      //printf("child exit code : %d\n",( status>>8)&0xFF);//child exit code 
      printf("child exit code : %d\n",WEXITSTATUS(status));//child exit code 
     // printf("child get signal: %d\n", status&0x7F);
     break;
    }
    else if(ret == 0) {
      printf("wait success!,child not quit!\n");
      sleep(1);
    }
    else{
      printf("waitpid fail!\n");
      break;
    }
   // printf("ret: %d\n", ret);
  }//end of while 

 // printf("father wait after!\n");
  }
}
